﻿using Raven;
using UnityEngine;
using Vector2 = Raven.Vector2;

public class PelletDisplay : UnityDisplayObject
{
    private ProjectilePellet _projectilePellet;


    public PelletDisplay(BaseGameEntity entity) : base(entity)
    {
        _projectilePellet = entity as ProjectilePellet;
    }

    public override void Start()
    {
        base.Start();
        _gameObject.SetActive(false);
    }


    public override void Update()
    {
        if (!_projectilePellet.isDead() && _projectilePellet.HasImpacted() && !_gameObject.activeSelf)
        {
            Vector2 pos = _projectilePellet.GetOrigin();
            Vector2 impactPoint = _projectilePellet.GetImpactPoint() - pos;

            _gameObject.transform.position = new Vector3((float)pos.x, (float)pos.y);
            _gameObject.AddComponent<MeshFilter>().mesh =
                MeshFactory.LineMesh(Vector3.zero, new Vector3((float)impactPoint.x, (float)impactPoint.y), Color.cyan);
            MeshRenderer render = _gameObject.AddComponent<MeshRenderer>();
            render.material = new Material(Shader.Find("Sprites/Default"));
            _gameObject.SetActive(true);

            GameObject circle = new GameObject("circle");
            circle.transform.SetParent(_gameObject.transform, false);
            circle.transform.localPosition = new Vector3((float)impactPoint.x, (float)impactPoint.y);
            circle.AddComponent<MeshFilter>().mesh = MeshFactory.CycleMesh(Vector3.zero, 3, Color.cyan);
            circle.AddComponent<MeshRenderer>().material = new Material(Shader.Find("Sprites/Default"));
        }
    }
}